﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WSIMWindowsClient.WSIMProfile;
using System.Security.Cryptography;
using WSIMCommons;

namespace WSIMWindowsClient
{
    public partial class Registration : Form
    {
        private RegisterInfo info = new RegisterInfo();
        WSIMProfile.WSIMProfileOperationsClient cl = new WSIMWindowsClient.WSIMProfile.WSIMProfileOperationsClient();

        public Registration()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            registerInfoBindingSource.DataSource = info;    
   
            //Add validation handlers
            userNameTextBox.Validating += new CancelEventHandler(fieldValidator);

            //WCF
            cl.ChannelFactory.Credentials.UserName.Password = "";
            cl.ChannelFactory.Credentials.UserName.UserName = "guest";
        }

        void fieldValidator(object sender, CancelEventArgs e)
        {
            //TODO: aggiungere validazione su username,email,privacy e controllare password
            //metodo "se (qualcosa va storto) error.SetError(nomeTextboxIncriminata,"meassaggio errore")"
        }

       
        private void cmdRegister_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
            info.Password = WSIMUtilities.SHA2(info.Password);
            RegisterUserRequest req = new RegisterUserRequest(info);
            cl.BeginRegisterUser(req, new AsyncCallback(this.CallBackEndRegistration), null);
            
        }


        private void CallBackEndRegistration(IAsyncResult ar) {            
            string s=cl.EndRegisterUser(ar).RegisterUserResult;

            //TODO: squallido avviso di completamento 
            MessageBox.Show(s);
            panel1.Visible = false;
        }

        
    }
}
